package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.bean.SpecialsellDeviceBean;

import java.util.List;

public class AutoDeviceListAdapter extends BaseAdapter {
    private Context context;        //上下文对象引用
    private List<SpecialsellDeviceBean> data;
    private AutoDeviceListAdapter.onClickButton onClickButton;
    public interface onClickButton {
        void detailsClick(SpecialsellDeviceBean bean);//查看详情
    }
    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public AutoDeviceListAdapter(Context context, List<SpecialsellDeviceBean> data,onClickButton onClickButton) {
        this.context = context;
        this.data = data;
        this.onClickButton  = onClickButton;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }


    @Override
    public Object getItem(int position) {
        return  data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_auto_device, null);
            viewHolder.tv_device_no = (TextView) convertView.findViewById(R.id.tv_device_no);
            viewHolder.tv_location = (TextView) convertView.findViewById(R.id.tv_location);
            viewHolder.detail = (TextView) convertView.findViewById(R.id.tv_detail);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(viewHolder);            //表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来
        }
        final SpecialsellDeviceBean specialsellDeviceBean = data.get(position);
        viewHolder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.v("zyl","onClick");
                onClickButton.detailsClick(specialsellDeviceBean);
            }
        });


        return convertView;
    }

    /**
     * 定义一个内部类
     * 声明相应的控件引用
     */
    public class ViewHolder {
        public ImageView iv;
        public TextView tv_device_no;            //设备号
        public TextView tv_location;            //设备位置
        public TextView detail;            //查看详情
    }
}
